<%@page contentType="text/html;charset=UTF-8" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">
<%@page import="java.text.NumberFormat" %>
<%@page import="be.destin.skos.core.SkosManager" %>
<%@page import="be.destin.skos.html.SkosHtmlManager" %>
<%@page import="be.destin.skos.html.SkosHtmlTable" %>
<%@page import="be.destin.skos.html.LocalError" %>
<%@page import="be.destin.skos.core.SchemeInterface" %>
<%@page import="be.destin.skos.core.SchemeUsage" %>
<%@page import="be.destin.skos.core.ConceptScheme" %>
<%@page import="be.destin.skos.core.Concept" %>
<%@page import="be.destin.skos.core.Application" %>
<%@page import="be.destin.skos.core.Term" %>
<%@page import="be.destin.skos.core.Profile" %>
<%@page import="be.destin.memolog.UsageMode" %><%@page import="be.destin.memolog.UsageLogger" %><%@page import="be.destin.memo.Log" %><%@page import="be.destin.memo.TagDef" %><%@page import="be.destin.memo.Tagged" %>
<%@page import="java.util.Map" %>
<%@page import="java.util.Map.Entry" %>
<%@page import="java.util.List" %>
<%@page import="java.util.LinkedList" %>
<%@page import="java.util.LinkedHashMap" %>
<%@page import="java.util.LinkedHashSet" %>
<%@page import="java.util.TreeMap" %>
<%@page import="be.destin.skos.view.SkosView" %><%@page import="be.destin.skos.view.SkosViewType" %><%@page import="be.destin.skos.view.ViewConcept" %><%@page import="be.destin.skos.view.SkosViewManager" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%	 
   SkosHtmlManager skosHtmlManager = SkosHtmlManager.getAskosiSessionInstance(request);
   SkosManager skosManager = skosHtmlManager.getSkosManager();
   String language = skosHtmlManager.getLanguage();
	String query = (String) pageContext.getSession().getAttribute("query");
	if (query==null) query="";
   String sort = request.getParameter("sort");
   String independant = request.getParameter("independant");
   String framing = request.getParameter("framed");
   boolean framed = framing != null && framing.equalsIgnoreCase("yes");
   if (independant != null && independant.equalsIgnoreCase("yes")) framed = false;
   String reciprocing = request.getParameter("reciprocal");
   boolean reciprocal = reciprocing != null && reciprocing.equalsIgnoreCase("yes");
   
   if (reciprocal) {
	for (SchemeInterface aScheme:skosManager.getConceptSchemes()) {
		if (aScheme instanceof ConceptScheme) {
			((ConceptScheme)aScheme).ensureReciprocalMatch();
		}
	}
   }

  TreeMap<String,SchemeInterface> SortSchemes = new TreeMap<String,SchemeInterface>();
  NumberFormat nf=NumberFormat.getInstance(); // Get Instance of NumberFormat
  nf.setMinimumIntegerDigits(9);  // The minimum Digits required is 9
  nf.setMaximumIntegerDigits(9); // The maximum Digits required is 9
  SkosHtmlTable skosHtmlTable = new SkosHtmlTable(skosHtmlManager);
  int nbColumns = 0;
  String modified = "";
  Profile userProfile = skosHtmlManager.getProfileData();
  int rank = 0;
  if (userProfile != null) {
  for (String aboutScheme: userProfile.getBrowsedList()) {
	ConceptScheme aScheme = skosManager.getConceptScheme(aboutScheme);
    if (aScheme != null) {
		nbColumns += skosHtmlTable.putScheme((ConceptScheme)aScheme);
		if ("alfa".equals(sort)) {
			 //SortSchemes.put(aScheme.getTitle(language).toUpperCase(),aScheme);
			 SortSchemes.put(aScheme.getAbout(),aScheme);
		} else if ("freq".equals(sort)) {
			 int total = ((ConceptScheme)aScheme).applicationUsageTotal();
/***
			 for (String anApplication: ((ConceptScheme)aScheme).getUsage().keySet()) {
				if (((ConceptScheme)aScheme).isApplicationUsageModified(anApplication)) {
					modified = modified + aScheme.getAbout()+":"+anApplication+" ";
					((ConceptScheme)aScheme).setApplicationUsageModified(anApplication,false);
					if (((ConceptScheme)aScheme).isApplicationUsageModified(anApplication)) modified = modified+"! ";
				 }
			}
 ***/
			 SortSchemes.put(nf.format(999999999-total)+aScheme.getAbout(),aScheme);
			 //SortSchemes.put(nf.format(999999999-aScheme.getSize())+aScheme.getAbout(),aScheme);
		} else { // Profile order
			 SortSchemes.put(nf.format(rank),aScheme);			
		}
	}
    rank++;
  }
  }
%>
<html>
<head>
<title><%=SortSchemes.size()%> concept schemes in <%=skosManager.getConfigDirectory()%></title>
<link rel="shortcut icon" href="image/tulipevertepetite.png" >
<link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/css/jqueryui.css" />
<link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/css/askosi.css" />
<link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/css/ajaxtags.css" />
<link rel="search" type="application/opensearchdescription+xml" href="<%=request.getContextPath()%>/concept.svc/opensearch.osdx"/>
    <script type="text/javascript" src="js/jquery-1.7.2.js"></script>
	<script type="text/javascript" src="js/jquery-ui-1.8.23.custom.js"></script>
    <script type="text/javascript" src="js/sessvars.js"></script>
    <script type="text/javascript" src="js/tablescroll.js"></script>
    <script type="text/javascript">
		sessvars.contextPath = "<%= request.getContextPath() %>";
		headerHeight=100;
		
		function getEventElement(e) {
			var targ;
			if (!e) var e = window.event;
			if (e.target) targ = e.target;
			else if (e.srcElement) targ = e.srcElement;
			if (targ.nodeType == 3) // defeat Safari bug
				targ = targ.parentNode;
			return targ;
		}

		function schemeList() {
			var result = '';
			$("#schemeList input[name=scheme]:checked").each(function(index,elem) {
			    result += elem.value+',';
			});
			return result;			
		};

		function focusOption() {
			var result = $("#schemeList input[name=focus]:checked");
			if (result && result.length > 0) return '&focus='+result[0].value;
			return "";			
		};

		$(function() {
			$( "#project" ).autocomplete({
				minLength: 2,
				source: 'autocomplete.jsp?scheme=',
				search: function (event, ui) {
					$( "#project" ).autocomplete( "option", "source", 'autocomplete.jsp?scheme='+schemeList()+focusOption());
					$( "#projectCode").val ("");
				},
				focus: function( event, ui ) {
					//$( "#project" ).val( ui.item.label );
					return false;
				},
				select: function( event, ui ) {
					$( "#project" ).val( ui.item.label );
					$( "#projectCode").val (ui.item.value);
					return false;
				}
			})
			.data( "autocomplete" )._renderItem = function( ul, item ) {
				return $( "<li></li>" )
					.data( "item.autocomplete", item )
					.append( "<a>"+(item.icon ? ('<img src=\"'+item.icon+'\"/>'):'')+ item.label + (item.size > 1 ? (' ('+item.size+')') : '') + (item.match ? ' &larr;<i>'+item.match+'</i>' : '') + "</a>" )
					.appendTo( ul );
			};
		});
	</script>
</head>
<body class="contentpage">
<div id="header">
	<%String pageID = "TOC";%><%@include file="basket.jnc"%>
	<%@include file="searchbox.jnc"%> 
</div>

<div id="content"><img src="image/carrevide.gif" height="100" border="0" alt="top margin"/><br style="clear:both"/>
<p><%=modified%></p>
<table cellspacing="0" class="tableWithFloatingHeader" id="schemeList"><col/><col class="SkosTableColumnOdd"/><col/><%=skosHtmlTable.toHtmlColGroups()%><%
	skosHtmlTable.initRow();%>
		<tr class="<%=(skosHtmlTable.nextRowClass())%>"><th colspan="3" align="left"><%if ("alfa".equals(sort)) { %>
 <img src="image/facetsortalfa.png" title="Alphabetical Sort" class="selectedaction" border="0"/> <a href="index.jsp?sort=freq"><img src="image/facetsortfreq.png" title="Frequency Sort" border="0" class="linkaction"/></a> <a href="index.jsp"><img src="image/sortdown.png" title="Suggested order" class="linkaction" border="0"/></a>
  <% } else if ("freq".equals(sort)){ %>
  <a href="index.jsp?sort=alfa"><img src="image/facetsortalfa.png" title="Alphabetical Sort" border="0" class="linkaction"/></a> <img src="image/facetsortfreq.png" title="Frequency Sort" border="0" class="selectedaction"/> <a href="index.jsp"><img src="image/sortdown.png" title="Suggested order" class="linkaction" border="0"/></a>
  <% } else { %>
  <a href="index.jsp?sort=alfa"><img src="image/facetsortalfa.png" title="Alphabetical Sort" border="0" class="linkaction"/></a> <a href="index.jsp?sort=freq"><img src="image/facetsortfreq.png" title="Frequency Sort" border="0" class="linkaction"/></a> <img src="image/sortdown.png" title="Suggested order" class="selectedaction" border="0"/> 
  <% } %>
  <form action="index.jsp" style="padding:0; margin:0; display:inline;"><select name="profile"><%=skosHtmlManager.toHtmlSelect(skosManager.getProfiles().getAllConcepts(),skosManager.getProfiles().getAbout_About(skosHtmlManager.getProfile())) %></select><input type="submit" value="set" class="linkaction"/></form>
  </th><%=skosHtmlTable.toHtmlApplicationHeaders()%></tr>
		<tr class="<%=skosHtmlTable.nextRowClass()%>"><th class="SkosTableRole"<%=("alfa".equals(sort)?" align=\"left\"":"")%>><%=("alfa".equals(sort)?"Scheme":"Freq.")%></th>
			<th><img src="image/concept16.gif" border="0"/></th><th align="left"> &nbsp; <img src="image/conceptscheme16.gif" border="0"/></th><%=skosHtmlTable.toHtmlRoleHeaders()%></tr><%
	long grandTotal = 0;
	long conceptTotal = 0;
	for (SchemeInterface aScheme:SortSchemes.values()) {
		int total =((ConceptScheme)aScheme).applicationUsageTotal();
		grandTotal += total;
		int schemeSize = aScheme.getSize();
		conceptTotal += schemeSize;
		%><tr class="<%=skosHtmlTable.nextRowClass()%>"><td class="<%=("alfa".equals(sort)?"SkosTableSchemeCode":"SkosTableSchemeSize")%>"><%=("alfa".equals(sort)?aScheme.getAbout():""+total)%></td>
		    <td class="SkosTableSchemeSize">
			<a href="view.jsp?scheme=<%=aScheme.getAbout()%>&display=concepts<%if(framed){%>&framed=yes<%}%>#concepts"<%-- target="askosi_scheme"--%>><%=schemeSize%></a>
		    <input type="checkbox" name="scheme" value="<%=aScheme.getAbout() %>"/></td><td class="SkosTableConcept">
			<a href="view.jsp?scheme=<%=aScheme.getAbout()%><%if(framed){%>&framed=yes<%}%>"<%-- target="askosi_scheme"--%>><%=skosHtmlManager.toHtmlImage(aScheme.getIconAdapted(skosHtmlManager),aScheme.getAbout(),false)%> <%=aScheme.getTitle(language)%></a>
			</td>
			<%=skosHtmlTable.toHtmlColumns(aScheme)%></tr><%
    }%>
	<tr class="<%=(skosHtmlTable.nextRowClass())%>"><th><%=grandTotal%></th><th class="SkosTableColumnOdd"><%=conceptTotal%></th><th align="right">Columns Totals: &nbsp; </th><%=skosHtmlTable.toHtmlTotalColumns()%></tr>
	<tr class="<%=(skosHtmlTable.nextRowClass())%>"><th>&nbsp;</th><th class="SkosTableColumnOdd">&nbsp</th><th align="right">Focus none:<input type="radio" name="focus" value="" checked/></th><%=skosHtmlTable.toHtmlSelectColumns()%></tr>
	<tr class="floatingHeaders"><th class="SkosTableRole"<%=("alfa".equals(sort)?" align=\"left\"":"")%>><%=("alfa".equals(sort)?"Scheme":"Freq.")%></th>
			<th>&nbsp;</th><th>&nbsp;</th><%=skosHtmlTable.toHtmlRoleHeaders()%></tr>
	<tr class="SkosTableFirstRow"><th colspan="<%=3+skosHtmlTable.getTotals().length%>"><form action="search.jsp" method="GET" id="schemeList"><%--
	   --%><label for="leftTruncation" alt="Left Truncation"><b>*</b><input type="checkbox" name="leftTruncation" value="on" checked/></label><input type="text" name="query" id="project" size="32"/><b>*</b>&nbsp;<%--
   	   --%><label for="byPieces" alt="By Pieces"><b>by pieces:</b><input type="checkbox" name="byPieces" value="on"/></label><%--
	   --%><input type="image" src="image/app/scheme.png" name="Search" alt="Search" class="linkaction" height="16"><a href="#" onclick="javascript:$('#wordsearch').val($('#project').val());return false;"><img src="image/search.png" name="Copy to Word Search" alt="Copy to Word Search" class="linkaction"/></a><input type="text" name="directabout" id="projectCode" size="20"/><img src="image/newbasket.png" border="0" title="New Basket" alt="New Basket" class="linkaction"/><%--
	--%></th></tr></table>
<%--br style="clear:both"/><img src="image/carrevide.gif" height="50" border="0" alt="bottom margin"/--%>
<p style="margin:6px; padding:2px;">
<a href="index.jsp?<%=(framed?"framed=yes&":"")%>reciprocal=yes" class="linkaction">Ensures Reciprocal Matching Relations when crossing Schemes boundaries</a>
<a href="analyze.jsp" class="linkaction">Analyze</a>
<a href="expand.jsp" class="linkaction">Expand</a>
</p><p style="margin:4px">Thesaurus navigation and maintenance, Indexation browsing are services developed in the <a href="http://www.askosi.org">ASKOSI</a> project.</p>
</div>
</body></html>